package com.api.shopapi.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.DeviceYjgateway;
import com.api.shopapi.entity.DeviceYjgatewayItem;
import com.api.shopapi.mapper.DeviceYjgatewayItemMapper;
import com.api.shopapi.mapper.DeviceYjgatewayMapper;
import com.api.shopapi.service.DeviceYjgatewayItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Service("DeviceYjgatewayItemServiceImplOne")
public class DeviceYjgatewayItemServiceImpl implements DeviceYjgatewayItemService {

    @Autowired
    private DeviceYjgatewayItemMapper deviceYjgatewayItemMapper;

    @Autowired
    private DeviceYjgatewayMapper deviceYjgatewayMapper;

    @Override
    @Transactional
    public void RefreshMeshDevice(String gwId, JSONArray devices) {

        DeviceYjgateway deviceYjgateway = deviceYjgatewayMapper.getByGatewayId(gwId);
        List<DeviceYjgatewayItem> deviceYjgatewayItemList = deviceYjgatewayItemMapper.getListByDeviceYjgatewayCode(deviceYjgateway.getDeviceYjgatewayCode());
        if (deviceYjgatewayItemList!=null&&deviceYjgatewayItemList.size()>0){
            for (DeviceYjgatewayItem deviceYjgatewayItem : deviceYjgatewayItemList) {
                deviceYjgatewayItemMapper.delete(deviceYjgatewayItem.getDeviceYjgatewayItemCode());
            }
        }

        for (int i=0;i<devices.size();i++){
            JSONObject device = (JSONObject) devices.get(i);
            DeviceYjgatewayItem deviceYjgatewayItem=new DeviceYjgatewayItem();
            deviceYjgatewayItem.setDeviceYjgatewayItemCode(UUID.randomUUID().toString());
            deviceYjgatewayItem.setDeviceYjgatewayCode(deviceYjgateway.getDeviceYjgatewayCode());
            deviceYjgatewayItem.setDeviceId(device.getString("deviceId"));
            deviceYjgatewayItem.setDeviceName(device.getString("deviceName"));
            deviceYjgatewayItem.setZone(device.getString("zone"));
            String model=device.getString("model");
            deviceYjgatewayItem.setModel(model);
            String deviceType="";
            if (model.equals("SY28016")){
                deviceType="MESH紧急按钮";
            }
            if (model.equals("SY28012")){
                deviceType="MESH红外探测器";
            }
            if (model.equals("SY28013")){
                deviceType="MESH门磁传感器";
            }
            if (model.equals("SY28014")){
                deviceType="MESH水浸传感器";
            }
            if (model.equals("SY05039")){
                deviceType="MESH光电烟雾探测器";
            }
            if (model.equals("SY05040")){
                deviceType="MESH燃气泄漏报警器";
            }
            deviceYjgatewayItem.setDeviceType(deviceType);
            deviceYjgatewayItemMapper.insert(deviceYjgatewayItem);
        }

    }

    @Override
    public List<DeviceYjgatewayItem> getListByDeviceYjgatewayCode(String deviceYjgatewayCode) {
        return deviceYjgatewayItemMapper.getListByDeviceYjgatewayCode(deviceYjgatewayCode);
    }
}
